<template>
  <view class="service-ticket-page page-content">
    <z-paging ref="paging" v-model="currentList" @query="queryList" :auto="false" :refresher-enabled="true"
      :loading-more-enabled="true" empty-view-text="暂无数据">
      <template #top>
        <up-navbar title="视频桩违停列表" :autoBack="false" :fixed="true" bgColor="transparent" :border="false" placeholder
          @leftClick="handleBackClick">
          <template #right>
            <view>新增</view>
          </template>
        </up-navbar>

        <up-sticky class="padding-20" bgColor="transparent">
          <view class="search-bar">
            <view class="search-bar-item" @click="showPicker1 = true">
              <text class="title">时间</text>
              <up-icon name="arrow-down"></up-icon>
            </view>

            <view class="time-picker">
              <text class="title"></text>
              <uni-datetime-picker type="daterange" v-model="dateRange" :border="false" @change="handleDateRangeChange"></uni-datetime-picker>
            </view>
            <up-picker :show="showPicker1" :columns="columns1" :closeOnClickOverlay="true"
              @confirm="handleDateTypeSelect" @cancel="showPicker1 = false" @close="showPicker1 = false"></up-picker>
            <up-datetime-picker closeOnClickOverlay :show="showDateTimePicker" v-model="formData.messageDate"
              mode="date" @cancel="hideDateTimePickerModal" @confirm="handleDateConfirm"
              @close="hideDateTimePickerModal"></up-datetime-picker>
          </view>

          <up-search v-model="searchKeyword" :clearabled="true" :showAction="true" placeholder="请输入泊位号/停车场" @search="handleSearch"></up-search>
          <up-tabs :list="tabsList" :scrollable="false" @change="handleTabChange"></up-tabs>
        </up-sticky>
      </template>

      <view class="service-ticket-list">
        <view class="list-item-card" v-for="(item, index) in currentList" :key="index">
          <view class="card-header">
            <view class="title">
              设备编号SN：<text class="number">{{ item.deviceSerial }}</text>
            </view>
          </view>
          <view class="card-content-box">
            <view class="content">
              <view class="item">
                <text class="label">
                  状态
                </text>
                <view class="value">
                  <view class="item-tag">
                    <up-tag :text="item.bizStatus == 0 ? '违章' : '未违章'" :type="item.bizStatus == 0 ? 'warning' : 'success'"></up-tag>
                  </view>
                </view>
              </view>
              <view class="item">
                <text class="label">
                  场地名称：
                </text>
                <text class="value">
                  {{ item.veName }}
                </text>
              </view>
              <view class="item">
                <text class="label">
                  来源：
                </text>
                <text class="value">
                  {{ ['巡检人员', '客服人员', '高位视频桩', '巡检车'][item.inType] || '未知' }}
                </text>
              </view>
              <view class="item">
                <text class="label">
                  泊位号：
                </text>
                <text class="value">
                  {{ item.berthNumbers }}
                </text>
              </view>
              <view class="item">
                <text class="label">
                  车牌号：
                </text>
                <text class="value">
                  {{ item.carPlate }}
                </text>
              </view>
            </view>
            <view class="card-footer">
              <!-- <view class="checkbox-box">
              </view> -->
              <view class="operate">
                <up-button type="default" text="指派任务" plain class="custom-style" @click="handleApply(item)"></up-button>
                <up-button type="primary" plain text="查看详情" class="custom-style" @click="handleDetail(item)"></up-button>
                <up-button type="primary" text="处理违章" class="custom-style" @click="handleCheck(item)"></up-button>
              </view>
            </view>
          </view>
        </view>
      </view>
    </z-paging>

    <CheckModal v-model:show="showCheckModal" :type="type" :rowData="editingData" />
    <ApplyModal v-model:show="showApplyModal" :type="type" :rowData="editingData" />
    <DetailModal v-model:show="showDetailModal" :rowData="editingData" />
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue';
import { getVideoStakeForbidList } from '@/api';
import CheckModal from './components/CheckModal.vue'
import ApplyModal from './components/ApplyModal.vue'
import DetailModal from './components/DetailModal.vue'

const { bool: showCheckModal, setTrue: openCheckModal } = useBoolean();
const { bool: showApplyModal, setTrue: openApplyModal } = useBoolean();
const { bool: showDetailModal, setTrue: openDetailModal } = useBoolean();
const { bool: showDateTimePicker, setTrue: showDateTimePickerModal, setFalse: hideDateTimePickerModal } = useBoolean();
const type = ref('add')
const editingData = ref({})
const formData = reactive({
  messageDate: Date.now(),
  isSelf: '',
})

const searchKeyword = ref('');
const selectedDateType = ref(0); // 默认近7天
const dateRange = ref([]);

// 选项卡数据  
const tabsList = [
  { name: '全部' },
  { name: '待指派' },
  { name: '待处理' },
  { name: '已处理' },
];
const currentTab = ref(0);
const showPicker1 = ref(false);

const columns1 = reactive([
  ['近7天', '近1个月', '近3个月', '近6个月', '近1年'],
]);
// z-paging 组件引用
const paging = ref(null)

// 当前显示的列表数据
const currentList = ref([])

// 选项卡切换事件
const handleTabChange = (e) => {
  const newIndex = e.index;
  if (currentTab.value === newIndex) {
    return; // 如果是同一个tab，不需要重新加载
  }

  currentTab.value = newIndex;

  // 切换tab时重新加载数据
  if (paging.value) {
    paging.value.reload();
  }
};

const handleCheck = (item) => {
  editingData.value = item
  openCheckModal()
}

const handleApply = (item) => {
  editingData.value = item
  openApplyModal()
}

// 处理查看详情按钮点击事件
const handleDetail = (item) => {
  console.log('查看详情', item)
  // 设置当前编辑的数据并打开详情弹窗
  editingData.value = item
  openDetailModal()
}

// 处理返回点击事件
const handleBackClick = () => {
  uni.redirectTo({
    url: '/pages/home/index'
  })
}


// 获取视频桩违章列表数据
const queryList = async (pageNo, pageSize) => {
  console.log(pageNo, pageSize, currentTab.value)

  const params = {
    pageNum: pageNo,
    pageSize,
    isAuthor: currentTab.value - 1, // 根据tab转换状态
    keyword: searchKeyword.value,
    dateType: selectedDateType.value
  }

  // 如果有日期范围，添加到参数中
  if (dateRange.value && dateRange.value.length === 2) {
    params.startDate = dateRange.value[0]
    params.endDate = dateRange.value[1]
  }

  await fetchList(params)
};

const fetchList = async (params) => {
  try {
    const result: any = await getVideoStakeForbidList(params)
    const data = result.data
    paging.value.complete(data.records || [])
  } catch (error) {
    paging.value.complete(false)
  }
}


// 搜索按钮点击事件
const handleSearch = () => {
  if (paging.value) {
    paging.value.reload();
  }
};

// 日期类型选择处理
const handleDateTypeSelect = (e) => {
  selectedDateType.value = columns1[0].indexOf(e.value[0]);
  showPicker1.value = false;
  if (paging.value) {
    paging.value.reload();
  }
};

// 日期范围选择处理
const handleDateRangeChange = (e) => {
  console.log('日期范围变更:', e);
  if (paging.value) {
    paging.value.reload();
  }
};

// 日期确认处理
const handleDateConfirm = (e) => {
  console.log('日期确认:', e);
  hideDateTimePickerModal();
  if (paging.value) {
    paging.value.reload();
  }
};

// 查看详情
const handleView = (row: any) => {
  // 跳转到详情页逻辑
  console.log('查看详情:', row);
};

// 审核操作
const handleVerify = (row: any) => {
  // 跳转到审核页逻辑
  console.log('审核操作:', row);
};

// 页面挂载时获取数据
onMounted(() => {
  fetchList({
    pageNum: 1,
    pageSize: 10,
  })
});
</script>

<style scoped lang="scss">
.service-ticket-list {
  padding: 20px;
}

.search-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0rpx 20rpx 30rpx;

  .time-picker {
    width: 400rpx;
    display: flex;
    align-items: center;

    .title {
      font-size: 28rpx;
      font-weight: bold;
      color: #333;
      margin-right: 10rpx;
    }
  }

  &-item {
    display: flex;
    flex: 1;
    justify-content: center;
    align-items: center;
    text-align: center;

    .item-icon {
      width: 40rpx;
      height: 40rpx;
      margin-right: 10rpx;
    }

    .title {
      font-size: 28rpx;
      font-weight: bold;
      color: #333;
      margin-right: 10rpx;
    }

    .arrow-icon {
      width: 30rpx;
      height: 30rpx;
    }
  }

  .waiting-count {
    font-weight: 500;
    font-size: 24rpx;
    color: #333333;
    display: flex;
    align-items: center;

    text {
      font-weight: bold;
      font-size: 36rpx;
      color: #fe6000;
      margin-left: 10rpx;
    }
  }
}

/* 列表统计 */
.list-statistics {
  display: flex;
  flex-wrap: wrap;
  padding: 20rpx 0;
}

.list-statistics .item {
  flex: 1 1 20%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
}

.list-statistics .label {
  font-size: 14px;
  color: #666;
}

.list-statistics .value {
  font-size: 18px;
  font-weight: bold;
  color: #e9710f;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}
</style>